package org.bigk.invoices.services;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.apache.commons.collections.CollectionUtils;
import org.bigk.invoices.exceptions.ServiceException;
import org.bigk.invoices.model.PurchaserRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("purchasersRolesService")
@Transactional
public class PurchasersRolesServiceImpl implements PurchasersRolesService {
	
	private static final Logger logger = LoggerFactory.getLogger(PurchasersRolesServiceImpl.class);

	private static final String ALL_PURCHASERS_ROLES_QUERY =
			"SELECT pr FROM PurchaserRole pr ORDER BY pr.id ASC";

	@PersistenceContext
	protected EntityManager em;

	@Override
	public List<PurchaserRole> listAllItems() throws ServiceException {
		logger.debug("listAllItems() - start");

		TypedQuery<PurchaserRole> query = em.createQuery(ALL_PURCHASERS_ROLES_QUERY, PurchaserRole.class);
		List<PurchaserRole> list = query.getResultList();

		logger.debug("listAllItems() - end - read [{}] element(s)", CollectionUtils.size(list));
		return list;
	}
	
	@Override
	public PurchaserRole getPurchaserRole(Long id) throws ServiceException {
		logger.debug("getPurchaserRole(id=[{}]) - start", + id);

		PurchaserRole purchaserRole = em.find(PurchaserRole.class, id);

		logger.debug("getPurchaserRole(Long) - end - return value=[{}]", purchaserRole);
		return purchaserRole;
	}
}
